package cn.example.configure;

import cn.example.interpector.GlobalInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 拦截器配置
 * Created by WuYubin on 2017/2/24.
 */
@Configuration
@ComponentScan("cn.example.project1")
public class InterceptorsConfig extends WebMvcConfigurerAdapter {

    @Bean
    GlobalInterceptor GlobalInterceptor() {
        return new GlobalInterceptor();
    }

    /**
     * 全局拦截器，根据不同的url来进入自定义的拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(GlobalInterceptor()).addPathPatterns("/**");
//        registry.addInterceptor(new AdminInterceptor()).excludePathPatterns("/admin/").excludePathPatterns("/admin/login").excludePathPatterns("/admin/doLogin").excludePathPatterns("/error").addPathPatterns("/admin/**");
        super.addInterceptors(registry);
    }

    /**
     * 跨域全局配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST")
                .maxAge(3600);
    }
}
